package com.camel.base.service.impl;

import com.camel.base.mapper.ZsQuestionMapper;
import com.camel.base.mapper.ZsSurveyMapper;
import com.camel.base.model.ZsOption;
import com.camel.base.model.ZsQuestion;
import com.camel.base.model.ZsSurvey;
import com.camel.base.service.ZsSurveyService;
import com.camel.core.entity.IFilter;
import com.camel.core.entity.IParams;
import com.camel.core.service.AbstractBaseCommonService;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.camel.base.utils.ExportExcelUtils.*;

/**
 * @author camel
 */
@Service
public class ZsSurveyServiceImpl extends AbstractBaseCommonService<ZsSurveyMapper, ZsSurvey> implements ZsSurveyService<ZsSurvey> {

    @Autowired
    private ZsSurveyMapper mapper;

    @Autowired
    private ZsQuestionMapper questionMapper;

    /**
     * 查询列表
     * @param params
     * @return
     */
    @Override
    public List<ZsSurvey> list(IParams params) {
        return mapper.selectIndex(buildWrapper(params));
    }

    public static String camel2under(String c) {
        String separator = "_";
        c = c.replaceAll("([a-z])([A-Z])", "$1"+separator+"$2").toLowerCase();
        return c;
    }

    @Override
    public XSSFWorkbook getImportTemplate(Integer id) {
        ZsSurvey survey = getById(id);
        List<ZsQuestion> zsQuestions = questionMapper.selectListBySurveyId(id);

        List hearder = new ArrayList<>();
        hearder.add("电话号码");
        hearder.addAll(getKeyFromText(survey.getStartShow()));
        hearder.addAll(getKeyFromText(survey.getEndShow()));
        for (ZsQuestion zsQuestion : zsQuestions) {
            hearder.addAll(getKeyFromText(zsQuestion.getName()));
            for (ZsOption child : zsQuestion.getChildren()) {
                hearder.addAll(getKeyFromText(child.getName()));
            }
        }
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("导入模板");
        CellStyle cellStyle = createCellStyle(wb);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFRow rowHaead = sheet.createRow(0);
        fillRow(rowHaead, createHeadStyle(wb), hearder);
        return wb;
    }

    public static List<String> getKeyFromText(String text) {
        ArrayList<String> r = new ArrayList<>();
        int i = text.indexOf("#{");
        if(i > -1) {
            int ie = text.indexOf("}", i);
            if(ie > 0) {
                String substring = text.substring(i + 2, ie);
                r.add(substring);
                r.addAll(getKeyFromText(text.substring(ie + 1, text.length())));
            }
        }
        return r;
    }

    public static void main(String[] args) {
        List<String> keyFromText = getKeyFromText("我是一个伟大的#{工作},那一天我带着我的#{工具}前往了#{地点}的中央");
        System.out.println(keyFromText);
    }
}




